<template>
	<view class="container hideToShow">
		<view class="form-warp">
			<uni-forms class="form" labelPosition="top">
				<!-- 用户姓名 -->
				<uni-forms-item class="form-item" label="用户姓名" label-width="200" required>
					<uni-easyinput v-model="user.username" placeholder="请输入用户姓名" :inputBorder="false" />
				</uni-forms-item>
				<!-- 性别 -->
				<uni-forms-item class="form-item" label="性别" label-width="200" required>
					<uni-data-checkbox v-model="user.sex" selectedColor="#48E7A5" selectedTextColor="#000" :localdata="sex"></uni-data-checkbox>
				</uni-forms-item>
				<!-- 出生年月 -->
				<uni-forms-item class="form-item" label="出生年月" label-width="200" required>
					<uni-datetime-picker v-model="user.birth" type="date" :border="false" :clear-icon="true" return-type="string" />
				</uni-forms-item>
				<!-- 用户身份证号 -->
				<uni-forms-item class="form-item" label="用户身份证号" label-width="200" required>
					<uni-easyinput v-model="user.idno" placeholder="请输入用户身份证号" :inputBorder="false" />
				</uni-forms-item>
				<!-- 用户手机号 -->
				<uni-forms-item class="form-item" label="用户手机号" label-width="200" required>
					<uni-easyinput v-model="user.phoneNum" placeholder="请输入用户手机号" :inputBorder="false" />
				</uni-forms-item>
				<!-- 电子邮箱 -->
				<uni-forms-item class="form-item" label="电子邮箱" label-width="200" required>
					<uni-easyinput v-model="user.email" placeholder="请输入电子邮箱地址" :inputBorder="false" />
				</uni-forms-item>
				<!-- 籍贯地址 -->
				<uni-forms-item class="form-item1" label="籍贯地址" label-width="200" required>
					<uni-easyinput v-model="user.address" type="textarea" placeholder="请输入籍贯地址" />
				</uni-forms-item>
			</uni-forms>
			<button class="next-btn" type="default" @tap="upload">提交</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 性别
			sex: [
				{
					text: '男',
					value: 1
				},
				{
					text: '女',
					value: 2
				}
			],

			// 个人信息
			user: {
				username: '', //用户姓名
				sex: '', //性别
				birth: '', //出生年月
				idno: '', //身份证号
				phoneNum: '', //手机号码
				email: '', //邮箱地址
				address: '' //籍贯地址
			}
		};
	},

	methods: {
		// 信息上传，进行下一步
		upload() {
			let sex = '';
			if (this.user.sex == 1) {
				sex = '男';
			} else if (this.user.sex == 2) {
				sex = '女';
			}

			// 校验的数据
			let checkUser = {
				username: this.user.username,
				idno: this.user.idno,
				phoneNum: this.user.phoneNum,
				email: this.user.email,
				sex: sex,
				birth: this.user.birth,
				address: this.user.address
			};

			if (this.$until.checkUser(checkUser)) {
				const callback = () => {
					uniCloud.callFunction({
						name: 'regist',
						data: {
							type: 'man',
							...checkUser
						},
						success: res => {
							console.log(res);
							if (!res.result.success) {
								uni.showToast({
									title: '该用户已注册',
									icon: 'none'
								});
							} else {
								uni.showToast({
									title: '注册成功'
								});
							}
						},
						fail: err => {
							uni.showToast({
								icon: 'none',
								title: '注册失败' + JSON.stringify(err)
							});
						}
					});
				};
				// 防抖节流
				this.$debounce(callback, 500);
			}
		}
	}
};
</script>

<style lang="less" scoped>
.container {
	width: 100%;
	min-height: 100vh;
	background-color: #fff;

	.form-warp {
		width: 100%;
		padding: 20rpx 48rpx;
		box-sizing: border-box;

		/deep/.is-required {
			margin-right: 8rpx;
		}

		.form-item {
			border-bottom: 2rpx solid #e8e8e8;
		}
	}

	.next-btn {
		color: #fff;
		margin-bottom: 44rpx;
		border-radius: 46rpx;
		background-color: #04d48e;
	}
}
</style>
